﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;
using System.Configuration;

namespace Props.CustomWeb.Models
{
    public class BaseModel
    {
        public FacebookSession FacebookSession { get; set; }
        public string VendorTitle { get; set; }
        public string BackgroundUrl { get; set; }
        public string BackgroundColor { get; set; }
    }

    public class FacebookSession
    {
        public static FacebookSession Current
        {
            get
            {
                string accessToken = HttpContext.Current.Session["AccessToken"] as string;

                if (accessToken != null)
                {
                    try
                    {
                        var client = new FacebookClient(accessToken);
                        dynamic result = client.Get("me", new { fields = "name,id,picture" });

                        string name = result.name;
                        string id = result.id;
                        string picture = result.picture.data.url;

                        HttpContext.Current.Session["FacebookId"] = id;
                        HttpContext.Current.Session["FacebookName"] = name;
                        HttpContext.Current.Session["FacebookPicture"] = picture;

                        return new FacebookSession
                        {
                            AccessToken = accessToken,
                            Id = id,
                            Name = name,
                            Picture = picture
                        };
                    }
                    catch (FacebookOAuthException)
                    {
                        accessToken = null;
                        HttpContext.Current.Session["AccessToken"] = null;
                        HttpContext.Current.Session["FacebookId"] = null;
                        HttpContext.Current.Session["FacebookName"] = null;
                        HttpContext.Current.Session["FacebookPicture"] = null;
                    }
                }
                else
                {
                    HttpContext.Current.Session["FacebookId"] = null;
                    HttpContext.Current.Session["FacebookName"] = null;
                    HttpContext.Current.Session["FacebookPicture"] = null;
                }

                return null;
            }
        }

        public string AccessToken { get; set; }
        public string Id { get; set; }
        public string Name { get; set; }
        public string Picture { get; set; }

        public static Uri LogOut(string host)
        {
            var current = Current;

            if (current != null)
            {
                var client = new FacebookClient(current.AccessToken);

                var uri = client.GetLogoutUrl(new 
                {
                    next = String.Format(ConfigSettings.Current.BaseUrlTemplate, host) + "/",
                    accessToken = current.AccessToken
                });

                HttpContext.Current.Session["AccessToken"] = null;

                return uri;
            }

            return null;
        }
    }
}